package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import domain.Contact;

/**
 * Servlet implementation class DeleteServlet
 */
public class DeleteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ApplicationContext context =
				WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		domain.IDAOContact daoContact = (domain.IDAOContact) context.getBean("DaoContactBean");
		String[] checkboxvalue=request.getParameterValues("idContact");
		Contact[] tabContact=(Contact[])getServletContext().getAttribute("contacts");
		String message=null;
		for(String c : checkboxvalue){
			message=daoContact.deleteContact(tabContact[Integer.valueOf(c)]);
			if(message.startsWith("Erreur")) break;
		}
		
		request.setAttribute("message", message);
		RequestDispatcher rd = request
				.getRequestDispatcher("pages/main.jsp");
		rd.forward(request, response);
	}

}
